package com.fc.push.handler.pub.verify;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.fc.persistence.cache.redis.SharedJedisFacade;
import com.fc.persistence.dao.DAOTemplate;
import com.fc.push.db.TranscationFacade;
import com.fc.push.db.entities.Application;
import com.fc.push.handler.AbstractHttpHandler;
import com.fc.push.handler.HandlerResult;
import com.fc.push.handler.pub.MessageRCode;
import com.fc.push.handler.pub.service.RedisServiceUtil;
import com.fc.remoting.anno.Protocol;

import redis.clients.jedis.Jedis;

@Component
@Protocol(module = VerifyConst.VERIFY_MODULE_ID, order = VerifyConst.ORDER_3)
public class PublishServiceUnRegisterHandler extends AbstractHttpHandler {
	private static final String APPKEY = "appkey";
	private static final String CLIENT_ID = "clientid";
	private static final String CODE = "code";

	@Autowired 
	RedisServiceUtil redisServiceUtil;
	
	@Override
	public Object handle(HttpServletResponse response, JSONObject request) {
		String appkey = request.getString(APPKEY);
		String clientid = request.getString(CLIENT_ID);

		if (appkey == null || appkey.isEmpty()) {
			JSONObject object = HandlerResult.result();
			object.put(CODE, MessageRCode.RCODE_ERROR_1002);
			return object;
		}

		if (clientid == null || clientid.isEmpty()) {
			JSONObject object = HandlerResult.result();
			object.put(CODE, MessageRCode.RCODE_ERROR_1002);
			return object;
		}

		String sql = "from " + Application.class.getSimpleName() + " where appkey = '" + appkey + "'";
		List<DAOTemplate> res = TranscationFacade.getInstance().transcation().getBySQL(sql);
		if (res == null || res.size() == 0) {
			JSONObject object = HandlerResult.result();
			object.put(CODE, MessageRCode.RCODE_ERROR_1008);
			return object;
		}
		redisServiceUtil.sremProxy(appkey, clientid);
		return HandlerResult.result();
	}
	
	
	public static void main(String[] args){
		
		String[] keys = {"9e569d3e25b04259bfbab1a13bc9276d","8543aaa4b3384d0c817a437b8b9d2a37",
				"c9a8d4b4c87c413ea385e03754f3082d","de25d601456b48ee871fed94d1e49b55","4d9b36ffde2746f9a38a08e2e1a1b1c5"};
		@SuppressWarnings("resource")
		Jedis jedis = new Jedis("112.74.101.75", 5657);
		for(int i=0;i<keys.length;i++){
			System.out.println(Arrays.toString(jedis.smembers(keys[i]).toArray()));
		}
		
	}
}
